package com.dfbz.dao;

import com.alibaba.excel.util.StringUtils;
import com.dfbz.entity.User;
import org.apache.ibatis.annotations.Param;

import javax.print.attribute.standard.RequestingUserName;
import java.util.Map;

public class LoginSqlProvider {

    public String findByUser(String username){
        StringBuilder sb = new StringBuilder();
        sb.append("select * from user u where u.username = #{username}");

        return sb.toString();
    }

    public String register(User user){
        StringBuilder sb = new StringBuilder();
        sb.append(" insert into xb.user (`username`,`password`,`email`) values (#{username},#{password},#{email})");
        return sb.toString();
    }


    public String findByEmailCount(String email){
        StringBuilder sb = new StringBuilder();
        sb.append("select COUNT(1) from user u where u.email= #{email}");

        return sb.toString();
    }

    public String findUserInfo(String userId){
        StringBuilder sb = new StringBuilder();
        sb.append("select u.pic,\n" +
                "u.look,\n" +
                "(select count(*) from userfocus where userfocus.user_id=#{userId}) as follow,\n" +
                "u.real_name,\n" +
                "d.`name`,\n" +
                "u.phone,\n" +
                "u.age,\n" +
                "u.gender,\n" +
                "u.register_time,\n" +
                "u.login_time,\n" +
                "u.is_secret\n" +
                "from `user` u \n" +
                "left JOIN dept d on u.dept_id=d.id\n" +
                "where u.id=#{userId}");
        return sb.toString();
    }

    public String selectPageCondition(Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT\n" +
                "`u`.id,\n" +
                "`u`.username,\n" +
                "`u`.`password`,\n" +
                "`u`.email,\n" +
                "`u`.qq_openid,\n" +
                "`u`.wx_openid,\n" +
                "`u`.real_name,\n" +
                "`u`.age,\n" +
                "`u`.phone,\n" +
                "`u`.gender,\n" +
                "`u`.description,\n" +
                "`u`.register_time,\n" +
                "`u`.login_time,\n" +
                "`u`.pic,\n" +
                "`u`.look,\n" +
                "`u`.is_secret,\n" +
                "`u`.dept_name,\n" +
                "`u`.dept_id\n" +
                "from user u ");
        //包含 value值 且 value值不为空
        if (params.containsKey("value")&& !StringUtils.isEmpty(params.get("value"))){
            //mybatis的sql处理语法  从map的key获取value值   #{key}
            sb.append("where username like concat('%',#{value},'%') ");
        }

        return sb.toString();
    }
}
